﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL.ReportBL;

public partial class Report_GetBalanceAccount : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("900600");
        if (!IsPostBack)
        {
            txtUserCode.Attributes.Add("onclick", "MultiSearchVIP('../');");

            DataSet ds = null;
            string ret=new BLL.ReportBL.BalanceAccount().GetOrder(out ds);
            drpOrder.DataSource = ds.Tables[0];
            drpOrder.DataBind();
            drpOrder.Items.Insert(0, new ListItem("全部", "全部"));

            drpSendFlag.Items.Add(new ListItem("未发放", "0"));
            drpSendFlag.Items.Add(new ListItem("已发放", "1"));
        }

        Session["FCode"] = "900600";
    }
    private void BindData(int pageIndex)
    {
        string qishustart = txtQISHUStart.Text;
        string qishuend = "";
        if (txtQISHUEnd.Text.Length > 0)
        {
            qishuend = txtQISHUEnd.Text + " 23:59:59:999";
        }

        string sharestart = txtShareStart.Text;
        string shareend = "";
        if (txtShareEnd.Text.Length > 0)
        {
            shareend = txtShareEnd.Text + " 23:59:59:999";
        }

        string givestart = txtGiveStart.Text;
        string giveend = "";
        if (txtGiveEnd.Text.Length > 0)
        {
            giveend = txtGiveEnd.Text + " 23:59:59:999";
        }

        BalanceAccount ba = new BLL.ReportBL.BalanceAccount();
        DataSet ds = null;

        string ret = ba.GetBalanceAccount(txtUserCode.Text,drpOrder.SelectedValue,int.Parse(drpSendFlag.SelectedValue),qishustart,qishuend,sharestart,shareend,givestart,giveend, out ds);

        if (ds != null)
        {
            DataView dv = new DataView(ds.Tables[0], "UserCode is not null", "", DataViewRowState.CurrentRows);

            if (pageIndex != 0) { 
            //导出EXcel文件
                DataTable2Excel(ds.Tables[0]); 
            }
            //if (dv.Count > 0)
            {
                grdBalance.PageIndex = pageIndex;
                grdBalance.DataSource = dv;
                grdBalance.DataBind();
            }

            if (dv.Count > 0)
            {
                dv = new DataView(ds.Tables[0], "UserCode is null", "", DataViewRowState.CurrentRows);
                grdBalance.FooterRow.Cells[6].Text = dv[0]["SENDFLAG"].ToString();
                grdBalance.FooterRow.Cells[7].Text = dv[0]["FC"].ToString();
            }
        }
    }

    /// <summary>
    /// DataTable导出Excel
    /// </summary>
    /// <param name="dtData"></param>
    public static void DataTable2Excel(System.Data.DataTable dtData)
    {
        System.Web.UI.WebControls.DataGrid dgExport = null;
        System.Web.HttpContext curContext = System.Web.HttpContext.Current;
        System.IO.StringWriter strWriter = null;
        System.Web.UI.HtmlTextWriter htmlWriter = null;

        if (dtData != null)
        {
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
            curContext.Response.Charset = "";

            dtData.Rows.RemoveAt(0);
            strWriter = new System.IO.StringWriter();
            htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);

            dgExport = new System.Web.UI.WebControls.DataGrid();

            dtData.Columns.Add("发放状态");
            //dtData.Columns.Add("财务审核");
            //dtData.Columns.Add("结算");


            dtData.Columns["USERCODE"].ColumnName = "会员代码";
            dtData.Columns["GKMC"].ColumnName = "会员名称";

            dtData.Columns["BILLID"].ColumnName = "关联单据";
            dtData.Columns["QISHU"].ColumnName = "期数";
            dtData.Columns["SHAREDATE"].ColumnName = "分摊日期";

            
            dtData.Columns["GIVEDATE"].ColumnName = "发放日期";
            dtData.Columns["FC"].ColumnName = "分成";
            //dtData.Columns["Sprice"].ColumnName = "标准单价";
            //dtData.Columns["Disc"].ColumnName = "折扣";
            //dtData.Columns["Aprice"].ColumnName = "实际单价";
            //dtData.Columns["Samt"].ColumnName = "标准金额";
            //dtData.Columns["Aamt"].ColumnName = "实际金额";

            for (int i = 0; i < dtData.Rows.Count; i++)
            {
                //dtData.Rows[i]["发货/入库"] = dtData.Rows[i]["SendOrReceive"].ToString() == "1" ? "已发货" : "未发货"; 
             
                dtData.Rows[i]["发放状态"] = dtData.Rows[i]["SENDFLAG"].ToString() == "1" ? "已发放" : "未发放";
                
            }
            ////移除列
            dtData.Columns.Remove(dtData.Columns["SENDFLAG"]); 


            dgExport.DataSource = dtData.DefaultView;
            dgExport.AllowPaging = false;
            dgExport.DataBind();

            dgExport.RenderControl(htmlWriter);
            curContext.Response.Write(strWriter.ToString());
            curContext.Response.End();
        }
    } 
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindData(0);
    }

    protected void btnExport_Click(object sender, EventArgs e)
    {
        BindData(1);//表示导出Excel
    } 

    protected void grdBalance_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[3].Text = e.Row.Cells[3].Text == "0" ? "未发放" : "已发放";
        }
    }
}
